Imports System.Data
Imports System.Collections
Imports System.Collections.Generic
Imports Microsoft.VisualBasic

Namespace MB.PsychCal.DAL
    Public MustInherit Class ArticlesProvider
        Inherits DataAccess


        Public Sub New()
            Me.ConnectionString = Globals.Settings.Articles.ConnectionString
            Me.EnableCaching = Globals.Settings.Articles.EnableCaching
            Me.CacheDuration = Globals.Settings.Articles.CacheDuration
        End Sub

        Private Shared _instance As ArticlesProvider = Nothing

        Public Shared ReadOnly Property Instance() As ArticlesProvider
            Get
                If IsNothing(_instance) Then
                    _instance = CType(Activator.CreateInstance(Type.GetType(Globals.Settings.Articles.ProviderType)), ArticlesProvider)
                End If

                Return _instance
            End Get
        End Property


        ' methods that work with articles
        Public MustOverride Function GetArticles(ByVal pageIndex As Integer, ByVal pageSize As Integer) _
               As List(Of ArticleDetails)
        Public MustOverride Function GetArticleCount() As Integer
        Public MustOverride Function GetArticleByID(ByVal articleID As Integer) As ArticleDetails
        Public MustOverride Function InsertArticle(ByVal article As ArticleDetails) As Integer
        Public MustOverride Function GetArticleBody(ByVal articleID As Integer) As String


        ' Returns a new ArticleDetails instance filled with the
        ' DataReader's current record data
        Protected Overridable Function GetArticlesFromReader(ByVal reader As IDataReader) _
                As ArticleDetails

            Return GetArticleFromReader(reader, True)
        End Function

        Protected Overridable Function GetArticleFromReader(ByVal reader As IDataReader, ByVal readBody As Boolean) _
                As ArticleDetails

            Dim article As New ArticleDetails( _
                  CInt(reader("announcementid")), _
                  reader("madeby").ToString, _
                  reader("subject").ToString, _
                  reader("message").ToString, _
                  CDate(reader("addeddate")), _
                  CBool(reader("isexpired")))
            Return article
        End Function

        ' Returns a collection of ArticleDetails objects with the 
        ' data read from the input DataReader
        Protected Overridable Function GetArticleCollectionFromReader(ByVal reader As IDataReader) _
                As List(Of ArticleDetails)

            Return GetArticleCollectionFromReader(reader, True)
        End Function

        Protected Overridable Function GetArticleCollectionFromReader( _
                ByVal reader As IDataReader, ByVal readBody As Boolean) _
                As List(Of ArticleDetails)
            Dim articles As New List(Of ArticleDetails)
            While (reader.Read())
                articles.Add(GetArticleFromReader(reader, readBody))
            End While
            Return articles
        End Function


    End Class
End Namespace
